<?php
	include_once "dataservice.php";
	class nguoidung_db
	{
		public function Them($MaLoaiNguoiDung, $TenDangNhap, $MatKhau, $HoTen, $DiaChi,
							 $DienThoai, $GioiTinh, $NgaySinh, $Email, $NgayThamGia, 
							 $MaTrangThai, $MaKichHoat, $Avatar)
		{
			$sql = "INSERT INTO 
								`tbl_nguoidung`(`MaLoaiNguoiDung`, `TenDangNhap`, 
								`MatKhau`, `HoTen`, `DiaChi`, `DienThoai`, `GioiTinh`, `NgaySinh`, 
								`Email`, `NgayThamGia`, `MaTrangThai`, `MaKichHoat`, `Avatar`) 
					VALUES ($MaLoaiNguoiDung, '$TenDangNhap', '$MatKhau', '$HoTen', '$DiaChi',
							 '$DienThoai', $GioiTinh, '$NgaySinh', '$Email', '$NgayThamGia', $MaTrangThai, '$MaKichHoat', '$Avatar')";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaNguoiDung,
								$MaLoaiNguoiDung, 
								$TenDangNhap, 
								$MatKhau, 
								$HoTen, 
								$DiaChi,
								$DienThoai, 
								$GioiTinh, 
								$NgaySinh, 
								$Email, 
								$NgayThamGia, 
								$MaTrangThai,
								$MaKichHoat,
								$Avatar)
		{
			$sql = "UPDATE `tbl_nguoidung` 
					SET 	`MaNguoiDung`    = '$MaNguoiDung',
							`MaLoaiNguoiDung`= '$MaLoaiNguoiDung',
							`MatKhau`        = '$MatKhau',
							`HoTen`          = '$HoTen',
							`DiaChi`         = '$DiaChi',
							`DienThoai`      = '$DienThoai',
							`GioiTinh`       = '$GioiTinh',
							`NgaySinh`       = '$NgaySinh',
							`Email`          = '$Email',
							`NgayThamGia`    = '$NgayThamGia',
							`MaTrangThai`    = '$MaTrangThai',
							`Avatar`         = '$Avatar' 
					WHERE 	`TenDangNhap`    = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function Xoa($MaNguoiDung = "")
		{
			$sql = "DELETE 	
					FROM `tbl_nguoidung` 
					WHERE 	MaNguoiDung = $MaNguoiDung";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaNguoiDung, $MaLoaiNguoiDung, $TenDangNhap, $MaTrangThai)
		{
			$sql = "SELECT * 
					FROM tbl_nguoidung 
					WHERE 1 ";
			
			if($MaNguoiDung != -1)
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";
			if($MaLoaiNguoiDung != -1)
				$sql .= " AND `MaLoaiNguoiDung` = '$MaLoaiNguoiDung' ";
			if($TenDangNhap != "")
				$sql .= " AND `TenDangNhap` = '$TenDangNhap' ";
			if($MaTrangThai != -1)
				$sql .= " AND `MaTrangThai` = '$MaTrangThai' ";
			$sql .= " ORDER BY `NgayThamGia` DESC"; //Bỏ qua người dùng là Admin
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayDanhSachPhanTrang($Start, $Limit, $MaNguoiDung, $MaLoaiNguoiDung, $TenDangNhap, $MaTrangThai)
		{
			$sql = "SELECT * 
					FROM tbl_nguoidung 
					WHERE 1 ";
			
			if($MaNguoiDung != -1)
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";
			if($MaLoaiNguoiDung != -1)
				$sql .= " AND `MaLoaiNguoiDung` = '$MaLoaiNguoiDung' ";
			if($TenDangNhap != "")
				$sql .= " AND `TenDangNhap` = '$TenDangNhap' ";
			if($MaTrangThai != -1)
				$sql .= " AND `MaTrangThai` = '$MaTrangThai' ";
			$sql .= " AND `MaNguoiDung` <> 1 ORDER BY `NgayThamGia` DESC LIMIT $Start, $Limit ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function LayHoTenTheoMa($MaNguoiDung)
		{
			$sql = "SELECT `HoTen` FROM `tbl_nguoidung` WHERE `MaNguoiDung`='$MaNguoiDung'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function LayDanhSachTheoMaNguoiDung($MaNguoiDung = "")
		{
			$sql = "SELECT * FROM tbl_nguoidung WHERE 1";
			
			if($MaNguoiDung != "")
				$sql .= " AND MaNguoiDung = '$MaNguoiDung'";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		public function CapNhatAvatar($TenDangNhap = "", $Avatar = "") 
		{
			$sql = "UPDATE `tbl_nguoidung` 
					SET `Avatar`='$Avatar' 
					WHERE `TenDangNhap`='$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayMatKhauTheoTenDangNhap($TenDangNhap = "") 
		{
			$sql = "SELECT `MatKhau` FROM `tbl_nguoidung` WHERE `TenDangNhap`='$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function LayHoTenTheoTenDangNhap($TenDangNhap = "") 
		{
			$sql = "SELECT `HoTen` FROM `tbl_nguoidung` WHERE `TenDangNhap`='$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function LayMaNguoiDungTheoTenDangNhap($TenDangNhap = "") 
		{
			$sql = "SELECT `MaNguoiDung` FROM `tbl_nguoidung` WHERE `TenDangNhap`='$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function CapNhatMatKhau($TenDangNhap, $MatKhau) 
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MatKhau`     = '$MatKhau' 
					WHERE  	`TenDangNhap` = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatMaKichHoat($TenDangNhap, $MaKichHoat) 
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaKichHoat`='$MaKichHoat' 
					WHERE 	`TenDangNhap` = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function KiemTraTonTai($MaNguoiDung, $TenDangNhap, $Email)
		{
			$sql = "SELECT 	COUNT(*) 
					FROM 	`tbl_nguoidung` 
					WHERE 	1 AND ( ";
			
			if($MaNguoiDung != -1)
				$sql .= " OR `MaNguoiDung` = '$MaNguoiDung'";
			if($TenDangNhap != "")
				$sql .= " OR `TenDangNhap` = '$TenDangNhap'";
			if($Email != "")
				$sql .= " OR `Email` = '$Email'";
			$sql .= " ) ";	
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function TonTaiTenDangNhap($TenDangNhap) 
		{
			$sql = "SELECT 	COUNT(*) 
					FROM 	`tbl_nguoidung` 
					WHERE 	`TenDangNhap` = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function TonTaiEmail($Email = "") 
		{
			$sql = "SELECT 	COUNT(*) 
					FROM 	`tbl_nguoidung` 
					WHERE 	`Email` = '$Email'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function LayTenDangNhapTheoEmail($Email = "") 
		{
			$sql = "SELECT 	TenDangNhap 
					FROM 	`tbl_nguoidung` 
					WHERE 	`Email` = '$Email'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function LayMaXacNhanTheoTenDangNhap($TenDangNhap) 
		{
			$sql = "SELECT 	`MaKichHoat` 
					FROM 	`tbl_nguoidung` 
					WHERE 	`TenDangNhap` = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteScalar($sql);
		}
		public function CapNhatLoaiNguoiDungTheoTenDangNhap($TenDangNhap, $MaLoaiNguoiDung)
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaLoaiNguoiDung`='$MaLoaiNguoiDung'
					WHERE 	TenDangNhap = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatMaTrangThaiTheoTenDangNhap($TenDangNhap, $MaTrangThai)
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaTrangThai` = '$MaTrangThai'
					WHERE 	`TenDangNhap`     = '$TenDangNhap'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatMaTrangThai($MaNguoiDung, $MaTrangThai)
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaTrangThai`     = '$MaTrangThai'
					WHERE 	`MaNguoiDung`     = '$MaNguoiDung'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatMaKichHoatTheoMaNguoiDung($MaNguoiDung, $MaKichHoat)
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaKichHoat`      = '$MaKichHoat'
					WHERE 	`MaNguoiDung`     = '$MaNguoiDung'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatLoaiNguoiDungTheoMaNguoiDung($MaNguoiDung, $MaLoaiNguoiDung)
		{
			$sql = "UPDATE 	`tbl_nguoidung` 
					SET 	`MaLoaiNguoiDung`      = '$MaLoaiNguoiDung'
					WHERE 	`MaNguoiDung`     = '$MaNguoiDung'";
			
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
	}
?>